﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using PloobsEngine.Utils;
using PloobsEngine.SceneControl;

namespace PloobsEngine.Light
{
    public class SpotLight : DeferredLight
    {   
        /// <summary>
        /// Serialization 
        /// </summary>
        public SpotLight()
        {
        }
        public SpotLight(Vector3 position, Vector3 direction, float coneDecay, float lightRadius, Color Color, float lightAngleCosine, float lightIntensity)
        {
            this.position = position;
            this.direction = direction;
            //this.strength = strength;
            this.lightRadius = lightRadius;
            this.coneDecay = coneDecay;
            this.lightAngleCosine = lightAngleCosine;
            this.lightIntensity = lightIntensity;
            this.color = Color;
            target = direction;            
            init();            
         }

        private void init()
        {
            this.ViewMatrix = Matrix.CreateLookAt(this.position, this.direction, Vector3.Up);
            float angle = (float)Math.Acos(this.lightAngleCosine);
            this.projMatrix = Matrix.CreatePerspectiveFieldOfView(angle * 2, 1, 1f, 1000f);
            _bias = 1.0f / 10000.0f;
        }

        public override void Serialize(System.Xml.XmlTextWriter textWriter)
        {
            SerializerHelper.SerializeBaseType<int>(getId(), "Id", textWriter);          
            SerializerHelper.SerializeVector3(Position, "LightPosition", textWriter);
            SerializerHelper.SerializeVector3(Direction, "LightDirection", textWriter);
            SerializerHelper.SerializeBaseType<float>(ConeDecay, "ConeDecay", textWriter);
            SerializerHelper.SerializeVector3(Color.ToVector3(), "Color", textWriter);
            SerializerHelper.SerializeBaseType<float>(LightRadius, "LightRadius", textWriter);
            SerializerHelper.SerializeBaseType<float>(LightIntensity, "LightIntensity", textWriter);
            SerializerHelper.SerializeBaseType<float>(LightAngleCosine, "LightAngleCosine", textWriter);
            SerializerHelper.SerializeBaseType<LightType>(LightType, "LightType", textWriter);
            SerializerHelper.SerializeVector3(Target, "Target", textWriter);            

        }
        public override void DeSerialize(System.Xml.XmlNode node, EngineStuff engine, IWorld world)
        {
            this.setId(SerializerHelper.DeserializeBaseType<int>("Id", node));
            lightRadius = SerializerHelper.DeserializeBaseType<float>("LightRadius", node);
            direction = SerializerHelper.DeserializeVector3("LightDirection", node);
            coneDecay = SerializerHelper.DeserializeBaseType<float>("ConeDecay", node);
            lightAngleCosine= SerializerHelper.DeserializeBaseType<float>("LightAngleCosine", node);

            lightIntensity = SerializerHelper.DeserializeBaseType<float>("LightIntensity", node);
            Color = new Color(SerializerHelper.DeserializeVector3("Color", node));
            position = SerializerHelper.DeserializeVector3("LightPosition", node);
            target = SerializerHelper.DeserializeVector3("Target", node);         
            init();
        }

        protected float lightIntensity;

        public override Matrix ProjMatrix
        {
            get
            {
                return this.projMatrix;
            }
            set
            {
                this.projMatrix= value;
            }
        }

        public override Matrix ViewMatrix
        {
            get
            {
                this.viewMatrix = Matrix.CreateLookAt(this.position, this.direction, Vector3.Up);
                return this.viewMatrix;
            }
            set
            {
                this.viewMatrix = value;
            }
        }

        public float LightIntensity
        {
            get { return lightIntensity; }
            set { lightIntensity = value; }
        }

        protected float lightAngleCosine;

        public float LightAngleCosine
        {
            get { return lightAngleCosine; }
            set { lightAngleCosine = value; }
        }
        
        protected Vector3 position;

        public virtual Vector3 Position
        {
            get { return position; }
            set { 
                position = value;                
            }
        }
        //private float strength;

        //public float Strength
        //{
        //    get { return strength; }
        //    set { strength = value; }
        //}
        protected Vector3 direction;

        public Vector3 Direction
        {
            get { return direction; }
            set { direction = value; }
        }
        protected float lightRadius;

        public float LightRadius
        {
            get { return lightRadius; }
            set { lightRadius = value; }
        }
        protected float coneDecay;

        public float ConeDecay
        {
            get { return coneDecay; }
            set { coneDecay = value; }
        }


        protected Vector3 target ;
        public virtual Vector3 Target
        {
            set
            {
                this.target = value;
                this.direction = (Vector3)target - (Vector3)position;
                this.direction.Normalize();
                

            }
            get
            {                
                return target;
            }
        }

        public override LightType LightType
        {
            get { return LightType.Deferred_Spot; }
        }
    }
}
